package com.buss.core.controller;

import java.util.HashMap;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.core.model.DataModel;
import com.buss.core.model.Sysmenu;
import com.buss.core.persistence.ResultList;
import com.buss.core.service.DataModelService;
import com.buss.core.sysparam.SysStaticParam;
import com.buss.core.validator.ResultMessage;

@Controller
@RequestMapping(value = "/admin")
public class DataModelController extends BaseController{

	@Autowired
	private DataModelService modelService;
	
	/**
	 * 页面数据模型列表页面
	 * @return
	 */
	@RequestMapping("/datamodel/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid){
		ModelAndView view = new ModelAndView("datamodel/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}
	
	/**
	 * 页面数据模型编辑页面
	 * @param type
	 * @param id
	 * @return
	 */
	@RequestMapping("/datamodel/{type}")
	public ModelAndView editPage(@PathVariable String type,@RequestParam(required=false) Long id){
		ModelAndView view = new ModelAndView("datamodel/edit");
		DataModel dataModel = new DataModel();
		if("editpage".equals(type)){
			dataModel = modelService.loadDataModel(id);
		}else{
			Sysmenu menu = new Sysmenu();
			menu.setId(id);
			dataModel.setMenu(menu);
		}
		view.addObject("dataModel", dataModel);
		return view;		
	}
	
	/**
	 * 返回排序页面
	 * @param type
	 * @param menuId
	 * @return
	 */
	@RequestMapping("/datamodel/{type}/{menuId}")
	public ModelAndView sortPage(@PathVariable String type,@PathVariable Long menuId){
		ModelAndView view = new ModelAndView("datamodel/sortpage");
		DataModel  dataModel = new DataModel();		
		String sortType = "list";
		if("listsortpage".equals(type)){
			dataModel.setListIsShow(true);
			sortType = "list";
		}else{
			dataModel.setEditIsShow(true);
			sortType = "edit";
		}
		ResultList<DataModel> data = modelService.queryDataModel(menuId, dataModel);
		view.addObject("dataModels", data.getData());
		view.addObject("sortType", sortType);
		return view;
	}
	
	@RequestMapping("/datamodel/updateSort")
	@ResponseBody
	public Map<String,Object> updateSort(@RequestParam String sortType, @RequestParam Long[] ids){
		if("list".equals(sortType)){
			modelService.updateListSort(ids);
		}else{
			modelService.updateEditSort(ids);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "更新模型顺序成功!"));
		return map;
	}
	
	/**
	 * 新增页面数据模型
	 * @param dataModel
	 * @return
	 */
	@RequestMapping("/datamodel/add")
	@ResponseBody
	public Map<String, Object> add( @Valid DataModel dataModel,BindingResult validResult){
		
		modelService.saveDataModel(dataModel);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "新增页面数据模型成功!"));
		
		return map;
	}
	
	/**
	 * 修改页面数据模型
	 * @param dataModel
	 * @return
	 */
	@RequestMapping("/datamodel/edit")
	@ResponseBody
	public Map<String, Object> edit( DataModel dataModel){
		modelService.editDataModel(dataModel);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("message", new ResultMessage("0", "修改页面数据模型成功!"));
		return map;
	}
	
	/**
	 * 删除页面数据模型信息
	 * @param ids
	 * @return
	 */
	@RequestMapping("/datamodel/delete")
	@ResponseBody
	public Map<String,Object> delete(@RequestParam Long[] ids){
		modelService.deleteDataModel(ids);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("0", "删除页面数据模型信息成功!");
		return map;
	}
	
	/**
	 * 获取页面数据模型列表信息
	 * @param menuId
	 * @param dataModel
	 * @return
	 */
	@RequestMapping("/datamodel/list/{menuId}")
	@ResponseBody
	public Map<String,Object> list(@PathVariable Long menuId, DataModel dataModel){
		
		ResultList<DataModel> resultList = modelService.queryDataModel(menuId, dataModel);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("result", resultList);
		map.put("message", new ResultMessage("0", "获取页面数据模型信息列表成功!"));
		return map;
	}
	
	
}
